package com.prp.security;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.struts2.ServletActionContext;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class Initialization {

	
	public static String initializeServerInfo(){
		
			

		Document doc = initDoc();
		
	
		
		String info = null;
		try{
			
			NodeList nodeList = doc.getElementsByTagName("server");
			Element node = (Element)nodeList.item(0);
			info = node.getElementsByTagName("host").item(0).getFirstChild().getNodeValue();
			info = info+","+node.getElementsByTagName("from").item(0).getFirstChild().getNodeValue();
			info = info+","+node.getElementsByTagName("username").item(0).getFirstChild().getNodeValue();
			info = info+","+node.getElementsByTagName("password").item(0).getFirstChild().getNodeValue();
			info = info+","+node.getElementsByTagName("personal").item(0).getFirstChild().getNodeValue();
			return info;
		}catch(Exception e){
			e.printStackTrace();
		}
		return info;
	}
	private static Document initDoc()
	{
		Document doc = null;
		
		try
		{
			String realPath = ServletActionContext.getServletContext().getRealPath("");
		
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
		
			doc = builder.parse(new File(realPath + "\\ServerInfo.xml"));
		}catch(Exception e){
			System.out.print(e.getMessage());
			return null;
		}
		return doc;
	}
	
	public static String getRelativeFilePath(){
		
		Document doc = initDoc();
		
		
		
		String info = "item_files";
		
		try{
									
			NodeList nodeList = doc.getElementsByTagName("filepath");
			Element node = (Element)nodeList.item(0);
			info = node.getElementsByTagName("path").item(0).getFirstChild().getNodeValue();
			
			return info;
		}catch(Exception e){
			e.printStackTrace();
		
		return info;
		
		}	
	}
}
